package com.easyfun.healthmagicbox.comm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.UserToPersonsListJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.UserToPersons;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;

public class UserToPersonsUpTask  extends BaseRedoTask implements Runnable,
								DatabaseAccess{
	String username;
	String personID;
	private String sharelist;
	private String retitle;
	
	//For redo task
	public UserToPersonsUpTask(Context context,CommCallback callback)
	{
		super(context,callback);
	}
	public UserToPersonsUpTask(String username,String personID,
			String retitle, String sharelist,	
			Context context,CommCallback callback)
	{

		super(context,callback);
		this.username = username;
		
		this.personID = personID;
		this.retitle = retitle;
		this.sharelist = sharelist;
	}
	
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}
	@Override
	public void run() {
		JSONObject serializedObj = new JSONObject();
		String result = "";
		try {
			serializedObj.put("username", username);
			serializedObj.put("personID", personID);
			serializedObj.put("retitle", retitle==null?"":retitle);
			serializedObj.put("sharelist", sharelist==null?"":sharelist);
			Map<String, String> parmas = new HashMap<String, String>();
			parmas.put("jsonstring", serializedObj.toString());
			result = super.uploadByHTTP(parmas, ConstantData.UserToPersonsUpURI);
			if (ServerResponseCode.parseOf(result) == ServerResponseCode.FAILED) {

				ccb.setCommResult(this, "",
						ServerResponseCode.FAILED);
				return;
			}else 
			{
				if ((result.length() > 0)) {
					if (ServerResponseCode.parseOf(result) == ServerResponseCode.SUCCESS) {
						ccb.setCommResult(this, result,
								ServerResponseCode.SUCCESS);
					} else {
						Dao<UserToPersons, Integer> utop = getHelper(mcontext)
								.getUserToPersonsDao();
						List<UserToPersons> datas =  
								UserToPersonsListJsonWrapper
								.getInstance(result,UserToPersonsListJsonWrapper.class).getMpps();

						if (datas.size() !=1 )
						{
							ccb.setCommResult(this, "",
									ServerResponseCode.FAILED);
							return;
						}
						UserToPersons up = datas.get(0);
						QueryBuilder<UserToPersons, Integer> queryBuilder = utop.queryBuilder();
						// find out the original one and modify it.
						queryBuilder.where().eq("personID", personID);
						List<UserToPersons> utops = queryBuilder.query();
						if (utops.size() == 1)
						{
							up.setId(utops.get(0).getId());
							utop.createOrUpdate(up);
							ccb.setCommResult(this, result,
									ServerResponseCode.SUCCESS);
						}
					}
				}
				ccb.setCommResult(this, "",
						ServerResponseCode.SUCCESS);
				return;
			}

		} catch (Exception e) {
			e.printStackTrace();
			ccb.setCommResult(this, result,
					ServerResponseCode.FAILED);
		}
		
	}
	@Override
	public String serializeParam() throws IllegalArgumentException{
		//find this kindof string: 
		// com.easyfun.healthmagicbox.comm.UserToPersonsUpTask^~14^~2^~null|#null|#null|#null
		// throw new IllegalArgumentException("param length is not 4");
		if ((this.personID == null)||(this.username==null))
		{
			throw new IllegalArgumentException("username or personID is null!");
		}
		return this.personID+BaseRedoTask.ContentsSeperator
				+this.username+BaseRedoTask.ContentsSeperator
				+this.retitle+BaseRedoTask.ContentsSeperator
				+this.sharelist;
	}
	@Override
	public void deserializeParam(String serialstring) throws IllegalArgumentException{
		String[] tmp = serialstring.split(BaseRedoTask.regExHead + BaseRedoTask.ContentsSeperator);
		if (tmp.length == 4)
		{
			personID = tmp[0];
			username = tmp[1];
			retitle = tmp[2];
			sharelist = tmp[3];
		}else
		{
			throw new IllegalArgumentException("param length is not 4");
		}
		
	}
		
	

}
